package com.yuyi.tool;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import com.corundumstudio.socketio.SocketIOClient;
import com.yuyi.model.KefuDO;
import com.yuyi.model.UserDTO;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@SuppressWarnings("unchecked")
public class MapCollectionHandler{
    private static Map<String, LinkedHashMap<String, UserDTO>> userMap = null;
    private static Map<String, LinkedHashMap<String, KefuDO>> kefuMap = null;
    private static Map<String, LinkedHashMap<String, UserDTO>> chatMap = null;
    private static Map<String, String> toUser = new HashMap<>();
    private static Map<String, SocketIOClient> clientMap = new HashMap<>();
  
    public static synchronized void putClientCollection(String name, SocketIOClient client)throws Exception{
        if (name == null) {
            throw new Exception();
        }
        name = name.trim();
        if (name.length() == 0) {
            throw new Exception();
        }
        clientMap.put(name, client);
    }
  
    public static synchronized SocketIOClient seleClientCollection(String name)throws Exception{
        if (name == null) {
            throw new Exception();
        }
        name = name.trim();
        if (name.length() == 0) {
            throw new Exception();
        }
        if ((clientMap != null) && (clientMap.size() > 0)){
            Set<String> set = clientMap.keySet();
            if (set.contains(name)){
                return (SocketIOClient)clientMap.get(name);
            }
        }
        return null;
    }
  
    public static synchronized String deleClientCollection(String name)throws Exception{
        if (name == null) {
            throw new Exception();
        }
        name = name.trim();
        if (name.length() == 0) {
            throw new Exception();
        }
        if ((clientMap != null) && (clientMap.size() > 0))
        {
            clientMap.remove(name);
            return "0";
        }
        return "1";
    }
  
    public static synchronized void puttoUserCollection(HttpServletRequest request, String kefuMail, String userid)throws Exception{
        if (request == null) {
            throw new Exception();
        }
        if (userid == null) {
            throw new Exception();
        }
        userid = userid.trim();
        if (userid.length() == 0) {
            throw new Exception();
        }
        if (kefuMail == null) {
            throw new Exception();
        }
        kefuMail = kefuMail.trim();
        if (kefuMail.length() == 0) {
            throw new Exception();
        }
        toUser.put(kefuMail, userid);
    }
  
    public static synchronized String seletoUserCollection(String kefuMail)throws Exception{
        if ((toUser != null) && (toUser.size() > 0)){
            String user = (String)toUser.get(kefuMail);
            return user;
        }
        return null;
    }
  
    public static synchronized String deletoUserCollection(String kefuMail)throws Exception{
        if (kefuMail == null) {
            throw new Exception();
        }
        kefuMail = kefuMail.trim();
        if (kefuMail.length() == 0) {
            throw new Exception();
        }
        if ((toUser != null) && (toUser.size() > 0))
        {
            toUser.remove(kefuMail);
            return "0";
        }
        return "1";
    }
  
    public static synchronized void putUserToCollection(HttpServletRequest request, String typeid, String userid, UserDTO user)throws Exception{
        if (request == null) {
            throw new Exception();
        }
        if (typeid == null) {
            throw new Exception();
        }
        typeid = typeid.trim();
        if (typeid.length() == 0) {
            throw new Exception();
        }
        if (userid == null) {
            throw new Exception();
        }
        userid = userid.trim();
        if (userid.length() == 0) {
            throw new Exception();
        }
        if (user == null) {
            throw new Exception();
        }
        ServletContext application = request.getServletContext();
        try{
            userMap = (Map<String, LinkedHashMap<String, UserDTO>>) application.getAttribute("userMap");
        }
        catch (Exception e){
            userMap = new HashMap<>();
        }
        if (userMap == null) {
            userMap = new HashMap<>();
        }
        LinkedHashMap<String, UserDTO> typeMap = (LinkedHashMap<String, UserDTO>)userMap.get(typeid);
        if (typeMap == null) {
            typeMap = new LinkedHashMap<String, UserDTO>();
        }
        typeMap.put(userid, user);
        userMap.put(typeid, typeMap);
        application.setAttribute("userMap", userMap);
    }
  
    public static synchronized String selectUserToCollection(String typeid, String userid)throws Exception{
        if (typeid == null) {
            throw new Exception();
        }
        typeid = typeid.trim();
        if (typeid.length() == 0) {
            throw new Exception();
        }
        if (userid == null) {
            throw new Exception();
        }
        userid = userid.trim();
        if (userid.length() == 0) {
            throw new Exception();
        }
        LinkedHashMap<String, UserDTO> typeMap = (LinkedHashMap<String, UserDTO>)userMap.get(typeid);      
        if ((typeMap != null) && (typeMap.size() != 0)){
            Set<String> set = typeMap.keySet();
            if (set.contains(userid)) {
                return "0";
            }
            return "1";
        }
        return "1";
    }
  
    public static synchronized void deleteUserToCollection(String typeid, String userid)throws Exception{
        if (typeid == null) {
            throw new Exception();
        }
        typeid = typeid.trim();
        if (typeid.length() == 0) {
            throw new Exception();
        }
        if (userid == null) {
            throw new Exception();
        }
        userid = userid.trim();
        if (userid.length() == 0) {
            throw new Exception();
        }
        LinkedHashMap<String, UserDTO> typeMap = (LinkedHashMap<String, UserDTO>)userMap.get(typeid);
        typeMap.remove(userid);
    }
  
    public static synchronized Integer selectUserToSizeCollection(HttpServletRequest request, String typeid)throws Exception{
        if (typeid == null) {
            throw new Exception();
        }
        typeid = typeid.trim();
        if (typeid.length() == 0) {
            throw new Exception();
        }
        ServletContext application = request.getServletContext();
        try{
            userMap = (Map<String, LinkedHashMap<String, UserDTO>>)application.getAttribute("userMap");
        }
        catch (Exception e){
            userMap = new HashMap<>();
        }
        if (userMap == null) {
            userMap = new HashMap<>();
        }
        LinkedHashMap<String, UserDTO> typeMap = (LinkedHashMap<String, UserDTO>)userMap.get(typeid);
        if ((typeMap != null) && (typeMap.size() != 0)) {
            return Integer.valueOf(typeMap.size());
        }
        return null;
    }
  
    public static synchronized void putKefuToCollection(String typeid, String kefuid, KefuDO kefu){ 
        System.out.println("2222222222");
        LinkedHashMap<String, KefuDO> typeMap = (LinkedHashMap<String, KefuDO>)kefuMap.get(typeid);     
        typeMap.put(kefuid, kefu);
        kefuMap.put(typeid, typeMap); 
        System.out.println(typeMap);
        System.out.println(kefuMap);
    }
  
    public static synchronized void deleteKefuToCollection(String typeid, String kefuid)throws Exception{
        if (typeid == null) {
            throw new Exception();
        }
        typeid = typeid.trim();
        if (typeid.length() == 0) {
            throw new Exception();
        }
        if (kefuid == null) {
            throw new Exception();
        }
        kefuid = kefuid.trim();
        if (kefuid.length() == 0) {
            throw new Exception();
        }
        LinkedHashMap<String, KefuDO> typeMap = (LinkedHashMap<String, KefuDO>)kefuMap.get(typeid);
        typeMap.remove(kefuid);
    }
  
    public static synchronized LinkedHashMap<String, KefuDO> searchKefuToCollection(HttpServletRequest request, String typeid)throws Exception{
        if (typeid == null) {
            throw new Exception();
        }
        typeid = typeid.trim();
        if (typeid.length() == 0) {
            throw new Exception();
        }
        ServletContext application = request.getServletContext();
        try{
            kefuMap = (Map<String, LinkedHashMap<String, KefuDO>>)application.getAttribute("kefuMap");
        }
        catch (Exception e){
            kefuMap = new HashMap<>();
        }
        if (kefuMap == null) {
            kefuMap = new HashMap<>();
        }
        LinkedHashMap<String, KefuDO> lhm = (LinkedHashMap<String, KefuDO>)kefuMap.get(typeid);
        return lhm;
    }
  
    public static synchronized String searchGRKefuToCollection(HttpServletRequest request, String typeid, String kefuMail)throws Exception{
        if (typeid == null) {
            throw new Exception();
        }
        typeid = typeid.trim();
        if (typeid.length() == 0) {
            throw new Exception();
        }
        ServletContext application = request.getServletContext();
        try{
            kefuMap = (Map<String, LinkedHashMap<String, KefuDO>>)application.getAttribute("kefuMap");
        }
        catch (Exception e){
            kefuMap = new HashMap<>();
        }
        if (kefuMap == null) {
            kefuMap = new HashMap<>();
        }
        LinkedHashMap<String, KefuDO> lhm = (LinkedHashMap<String, KefuDO>)kefuMap.get(typeid);
        if ((lhm != null) && (lhm.size() != 0)){
            Set<String> set = lhm.keySet();
            if (set.contains(kefuMail)) {
                return "0";
            }
            return "1";
        }
        return "1";
    }
  
    public static synchronized void putChatUserToCollection(HttpServletRequest request, String typeid, String userid, UserDTO user)throws Exception{
        if (request == null) {
          throw new Exception();
        }
        if (typeid == null) {
          throw new Exception();
        }
        typeid = typeid.trim();
        if (typeid.length() == 0) {
          throw new Exception();
        }
        if (userid == null) {
          throw new Exception();
        }
        userid = userid.trim();
        if (userid.length() == 0) {
          throw new Exception();
        }
        if (user == null) {
          throw new Exception();
        }
        ServletContext application = request.getServletContext();
        try{
            chatMap = (Map<String, LinkedHashMap<String, UserDTO>>)application.getAttribute("chatMap");
        }
        catch (Exception e){
            chatMap = new HashMap<>();
        }
        if (chatMap == null) {
            chatMap = new HashMap<>();
        }
        LinkedHashMap<String, UserDTO> typeMap = (LinkedHashMap<String, UserDTO>)chatMap.get(typeid);
        if (typeMap == null) {
            typeMap = new LinkedHashMap<String, UserDTO>();
        }
        typeMap.put(userid, user);   
        chatMap.put(typeid, typeMap);
        application.setAttribute("chatMap", chatMap);
    }
  
    public static synchronized String selectChatUserToCollection(String typeid, String userid)throws Exception{
        if (typeid == null) {
            throw new Exception();
        }
        typeid = typeid.trim();
        if (typeid.length() == 0) {
            throw new Exception();
        }
        if (userid == null) {
            throw new Exception();
        }
        userid = userid.trim();
        if (userid.length() == 0) {
            throw new Exception();
        }
        LinkedHashMap<String, UserDTO> typeMap = (LinkedHashMap<String, UserDTO>)chatMap.get(typeid);
        if ((typeMap != null) && (typeMap.size() != 0)){
            Set<String> set = typeMap.keySet();      
            if (set.contains(userid)) {
                return "0";
            }
            return "1";
        }
        return "1";
    }
  
    public static synchronized List<String> seleChatUserToCollection(HttpServletRequest request, String kefuMail)throws Exception{
        if (request == null) {
            throw new Exception();
        }
        if (kefuMail == null) {
            throw new Exception();
        }
        kefuMail = kefuMail.trim();
        if (kefuMail.length() == 0) {
            throw new Exception();
        }
        ServletContext application = request.getServletContext();
        try{
            chatMap = (Map<String, LinkedHashMap<String, UserDTO>>)application.getAttribute("chatMap");
        }
        catch (Exception e){
            chatMap = new HashMap<>();
        }
        if (chatMap == null) {
            chatMap = new HashMap<>();
        }
        List<String> list = new ArrayList<>();
        LinkedHashMap<String, UserDTO> typeMap = (LinkedHashMap<String, UserDTO>)chatMap.get(kefuMail);
        if ((typeMap != null) && (typeMap.size() != 0)){
            Set<String> set = typeMap.keySet();
            if (set != null){
                list.addAll(set);
                return list;
            }
        }
        return list;
    }
  
    public static synchronized UserDTO selectChatUserToUserCollection(String typeid, String userid)throws Exception{
        if (typeid == null) {
            throw new Exception();
        }
        typeid = typeid.trim();
        if (typeid.length() == 0) {
            throw new Exception();
        }
        if (userid == null) {
            throw new Exception();
        }
        userid = userid.trim();
        if (userid.length() == 0) {
            throw new Exception();
        }
        LinkedHashMap<String, UserDTO> typeMap = (LinkedHashMap<String, UserDTO>)chatMap.get(typeid);
        if ((typeMap != null) && (typeMap.size() != 0)){
            Set<String> set = typeMap.keySet();       
            if (set.contains(userid)){
                UserDTO user = (UserDTO)typeMap.get(userid);
                return user;
            }
            return null;
        }
        return null;
    }
  
    public static synchronized Integer selectChatUserToSizeCollection(HttpServletRequest request, String kefuMail)throws Exception{
        if (kefuMail == null) {
            throw new Exception();
        }
        kefuMail = kefuMail.trim();
        if (kefuMail.length() == 0) {
            throw new Exception();
        }
        ServletContext application = request.getServletContext();
        try{
            chatMap = (Map<String, LinkedHashMap<String, UserDTO>>)application.getAttribute("chatMap");
        }
        catch (Exception e){
            chatMap = new HashMap<>();
        }
        if (chatMap == null) {
            chatMap = new HashMap<>();
        }
        LinkedHashMap<String, UserDTO> typeMap = (LinkedHashMap<String, UserDTO>)chatMap.get(kefuMail);
        if ((typeMap != null) && (typeMap.size() != 0)) {
            return Integer.valueOf(typeMap.size());
        }
        return null;
    }
  
    public static synchronized void deleteChatUserToCollection(String typeid, String userid)throws Exception{
        if (typeid == null) {
            throw new Exception();
        }
        typeid = typeid.trim();
        if (typeid.length() == 0) {
            throw new Exception();
        }
        if (userid == null) {
          throw new Exception();
        }
        userid = userid.trim();
        if (userid.length() == 0) {
            throw new Exception();
        } 
        LinkedHashMap<String, UserDTO> typeMap = (LinkedHashMap<String, UserDTO>)chatMap.get(typeid);
        typeMap.remove(userid);
    }
  
    public static synchronized UserDTO getNextUser(HttpServletRequest request, String typeid)throws Exception{
        if (request == null) {
            throw new Exception();
        }
        if (typeid == null) {
            throw new Exception();
        }
        typeid = typeid.trim();
        if (typeid.length() == 0) {
            throw new Exception();
        }
        ServletContext application = request.getServletContext();
        try{
            userMap = (Map<String, LinkedHashMap<String, UserDTO>>)application.getAttribute("userMap");
        }
        catch (Exception e){
            throw new Exception();
        }
        if (userMap == null) {
            throw new Exception();
        }
        LinkedHashMap<String, UserDTO> typeMap = (LinkedHashMap<String, UserDTO>)userMap.get(typeid);
        if (typeMap == null) {
            throw new Exception();
        }
        int size = typeMap.size();
        if (size == 0) {
            return null;
        }
        Set<String> set = typeMap.keySet();
        Iterator<String> iterator = set.iterator();
        String userid = (String)iterator.next();
        UserDTO user = (UserDTO)typeMap.get(userid);
        return user;
    }
  
    public static JSONArray doUserMapToJSON(HttpServletRequest request, String kefuType)throws Exception{
        ServletContext application = request.getServletContext();
        try{
            userMap = (Map<String, LinkedHashMap<String, UserDTO>>)application.getAttribute("userMap");
        }
        catch (Exception e){
            userMap = new HashMap<>();
        }
        if (userMap == null) {
            userMap = new HashMap<>();
        }
        if (userMap.size() == 0) {
            return null;
        }
        JSONArray typeJson = new JSONArray();
        LinkedHashMap<String, UserDTO> listMap = (LinkedHashMap<String, UserDTO>)userMap.get(kefuType);
        if ((listMap != null) && (listMap.size() != 0)){
            Set<String> set = listMap.keySet();
            Iterator<String> it = set.iterator();
            while (it.hasNext()){
                String userid = (String)it.next();
                UserDTO user = (UserDTO)listMap.get(userid);
                typeJson.add(user);
            }
        }
        return typeJson;
    }
  
    public static JSONArray doChatUserMapToJSON(HttpServletRequest request, String kefuMail)throws Exception{
        ServletContext application = request.getServletContext();
        try{
            chatMap = (Map<String, LinkedHashMap<String, UserDTO>>)application.getAttribute("chatMap");
        }
        catch (Exception e){
            chatMap = new HashMap<>();
        }
        if (chatMap == null) {
            chatMap = new HashMap<>();
        }
        if (chatMap.size() == 0) {
            return null;
        }
        JSONArray typeJson = new JSONArray();
        LinkedHashMap<String, UserDTO> typeMap = (LinkedHashMap<String, UserDTO>)chatMap.get(kefuMail);
        if ((typeMap != null) && (typeMap.size() != 0)){
            Set<String> set = typeMap.keySet();
            Iterator<String> it = set.iterator();
            while (it.hasNext()){
                String userid = (String)it.next();
                UserDTO user = (UserDTO)typeMap.get(userid);
                typeJson.add(user);
            }
        }
        return typeJson;
    }
  
    public static JSONObject doKefuMapToJSON(HttpServletRequest request, String[] typeids, String kefuMail)throws Exception{
        ServletContext application = request.getServletContext();
        try{
            kefuMap = (Map<String, LinkedHashMap<String, KefuDO>>)application.getAttribute("kefuMap");
        }
        catch (Exception e){
            kefuMap = new HashMap<>();
        }
        if (kefuMap == null) {
            kefuMap = new HashMap<>();
        }
        if (kefuMap.size() == 0) {
            return null;
        }
        JSONObject userJson = new JSONObject();
        for (int i = 0; i < typeids.length; i++){
            String typeid = typeids[i];
            LinkedHashMap<String, KefuDO> typeMap = (LinkedHashMap<String, KefuDO>)kefuMap.get(typeid);
            if ((typeMap != null) && (typeMap.size() != 0)){
                JSONArray typeJson = new JSONArray();
                Set<String> set = typeMap.keySet();
                Iterator<String> it = set.iterator();
                while (it.hasNext()){
                    String kefuid = (String)it.next();
                    if (!kefuid.equals(kefuMail)){
                        KefuDO kefu = (KefuDO)typeMap.get(kefuid);
                        typeJson.add(kefu);
                    }
                }
                if ((typeJson != null) && (typeJson.size() != 0)) {
                    userJson.element(typeid, typeJson);
                }
            }
        }
        return userJson;
    }
  
    public static JSONArray doKefuMapToGUIJSON(HttpServletRequest request, String[] typeids, String kefuMail)throws Exception{
        ServletContext application = request.getServletContext();
        try{
            kefuMap = (Map<String, LinkedHashMap<String, KefuDO>>)application.getAttribute("kefuMap");
        }
        catch (Exception e){
            kefuMap = new HashMap<>();
        }
        if (kefuMap == null) {
            kefuMap = new HashMap<>();
        }
        if (kefuMap.size() == 0) {
            return null;
        }
        JSONArray typeJson = new JSONArray();
        for (int i = 0; i < typeids.length; i++){
            String typeid = typeids[i];
            LinkedHashMap<String, KefuDO> typeMap = (LinkedHashMap<String, KefuDO>)kefuMap.get(typeid);
            if ((typeMap != null) && (typeMap.size() != 0)){
                Set<String> set = typeMap.keySet();
                Iterator<String> it = set.iterator();
                while (it.hasNext()){
                    String kefuid = (String)it.next();
                    if (!kefuid.equals(kefuMail)){
                        KefuDO kefu = (KefuDO)typeMap.get(kefuid);
                        typeJson.add(kefu);
                    }
                }
            }
        }
        return typeJson;
    }
}
